// sub/user/delivery-apply/delivery-apply.js
import Toast from '../../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
import Dialog from '../../../miniprogram/miniprogram_npm/vant-weapp/dialog/dialog'

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: '',
    realname: '',
    mobile: '',
    sex: '',
    sexOptions: [
      {text: '男', value: 1},
      {text: '女', value: 2}
    ],
    disBtn: false,
    textBtn: '提交申请'
  },

  nameBlur(event){
    this.setData({  realname: event.detail })
  },
  mobileBlur(event){
    this.setData({ mobile: event.detail })
  },
  //性别监听
  sexChange(event){
    this.setData({ sex: event.detail })
  },

  //提交
  saveSubmit(){
    let _this = this;
    if(!_this.data.realname){
      Toast('请填写真实姓名'); return;
    } else if(!_this.data.mobile || !(/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(_this.data.mobile)) ){
      Toast('请填写正确电话'); return;
    } else if(!_this.data.sex){
      Toast('请选择性别'); return;
    }

    app.requestUrl({
      method: 'POST',
      url: '/wxapi/deliveryman/application',
      data: {
        userId: _this.data.user.id,
        userName: _this.data.user.nickname,
        name: _this.data.realname,
        sex: _this.data.sex,
        mobile: _this.data.mobile,
      },
      header: {
        'Content-Type': 'application/json;charset=UTF-8',
        'Authorization': wx.getStorageSync('authToken') || 'empty-token'
      },
    }).then(res =>{
      Dialog.alert({
        title: '提示',
        message: '申请已提交，请等待审核'
      }).then(()=>{
        wx.navigateBack({detail: 1})
      })
    })
  },
  //检查配送员
  getQuery(){
    let _this = this;
    app.requestUrl({
      method: 'GET',
      url: '/wxapi/deliveryman/query',
      data: {
        userId: _this.data.user.id
      }
    }).then(res =>{
      if(res.data.deliveryman){
        _this.data.realname = res.data.deliveryman.name;
        _this.setData({ realname: _this.data.realname })
        if(res.data.deliveryman.auditStatus == 0){
          _this.data.textBtn = '正在审核中';
          _this.data.disBtn = true;
        } else if(res.data.deliveryman.auditStatus == 1){
          _this.data.textBtn = '配送员';
          _this.data.disBtn = true;
        } else if(res.data.deliveryman.auditStatus == 2){
          _this.data.textBtn = '审核未通过，再次提交';
        }
      }
      _this.setData({
        textBtn: _this.data.textBtn,
        disBtn: _this.data.disBtn,
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user: app.getUser(),
      mobile: app.getUser().mobile,
      sex: app.getUser().gender,
    })
    this.getQuery();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})